<?php

/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * =========================================================
 */

namespace app\model\fenxiao;

use app\model\BaseModel;
use app\model\member\MemberAccount as MemberAccountModel;

/**
 * 普通（快递）订单
 *
 * @author Administrator
 *
 */
class Fenxiao extends BaseModel
{
    /**
     * 添加分销账单
     * @param unknown $data
     */
    public function add($data){
        $member = model('member')->getInfo([ 'member_id' => $data[ 'member_id' ] ], '*');
        if(empty($member[ 'source_member' ])){
            return;
        }
        $p_member_info = model('member')->getInfo([ 'member_id' => $member[ 'source_member' ] ], '*');
        
        $p_member_level = model('member_level')->getInfo([ 'level_id' => $p_member_info[ 'member_level' ] ], '*');
        
        if($p_member_info['is_user']==0){
            //return;
        }
        $commission = '';
        $commission_rate = '';
        $one_fenxiao_id = $member[ 'member_id' ];
        $one_rate = $p_member_level['one_rate']/100;
        $one_commission = round($data['goods_money']*$one_rate,2);
        $one_fenxiao_name = $member['username'];
        $insert_data = [
                'order_id'=>$data['order_id'],
                'order_no'=>$data['order_no'],
                'order_goods_id'=>$data['order_goods_id']??'',
                'site_id'=>$data['site_id'],
                'site_name'=>$data['site_name'],
                'member_id'=>$p_member_info['member_id'],
                'member_name'=>$p_member_info['nickname'],
                'member_mobile'=>$p_member_info['mobile'],
                'full_address'=>$p_member_info['full_address'],
                'commission'=>$commission,
                'commission_rate'=>$commission_rate,
                'one_fenxiao_id'=>$one_fenxiao_id,
                'one_rate'=>$one_rate,
                'is_settlement'=>0,
                'is_refund'=>0,
                'create_time'=>time(),
                'before_balance'=>$p_member_info['balance'],
                'balance'=>$p_member_info['balance'],
                'after_balance'=>$p_member_info['balance']+$one_commission,
            ];
        
        $result = model("fenxiao_order")->add($insert_data);    
        return $this->success($result);
    }

    public function ok($order_info){
        
        $p_fenxiao_order_info = model('fenxiao_order')->getInfo([ 'order_id' => $order_info[ 'order_id' ] ], '*');
        
        $p_member_info = model('member')->getInfo([ 'member_id' =>  $p_fenxiao_order_info[ 'member_id' ] ], '*');
        $data = [
                'is_settlement'=>1,
                'balance'=>$p_member_info['balance'],
                'after_balance'=>$p_member_info['balance']+$p_fenxiao_order_info['commission']
            ];
        
        model('fenxiao_order')->update($data, ['order_id'=>$order_info[ 'order_id' ]]);
        model('member')->update(['balance'=>$data['after_balance']], ['member_id'=>$p_member_info[ 'member_id' ]]);
        
        
        $member_account_model = new MemberAccountModel();
        $member_account_model->addMemberAccount($data['site_id'], $p_member_info['member_id'], 'balance', $one_commission, 'adjust', 0,   '分销提成');
       
    }

  
}